package com.xph.core.common.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 常用工具
 * @author Administrator
 *
 */
public class CommUtil implements ApplicationContextAware {
    
    private static Logger log = Logger.getLogger(CommUtil.class);
    
    private static ApplicationContext ctx;
    
    public static HashMap retvalMap = new HashMap();//错误码配置
    public static HashMap syscfgMap = new HashMap();//系统配置项

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CommUtil.ctx = applicationContext;
    }   
    
    /**
     * 获取spring bean
     * @param name
     * @return
     */
    public static Object getBean(String name){
        return ctx.getBean(name);
    }
    /** 
     * 提供精确的加法运算。 
    *  
     * @param v1 
     *            被加数 
     * @param v2 
     *            加数 
     * @return 两个参数的和 
     */  
  
    public static double add(double v1, double v2)  
    {  
        BigDecimal b1 = new BigDecimal(Double.toString(v1));  
        BigDecimal b2 = new BigDecimal(Double.toString(v2));  
        return b1.add(b2).doubleValue();  
   }  

    
    
    /**
     * 将List<Map>里面的key转换为小写
     * @param list
     * @return
     */
    public static List lowerListMapKey(List<Map<String, Object>> list) {
        List newList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            newList.add(lowerMapKey(list.get(i)));
        }
        return newList;
    }
    
    /**
     * 把Map里面的key转换为小写
     * @param map
     * @return
     */
    public static Map lowerMapKey(Map<String ,Object> map){
        Map newMap = new HashMap();
        if(map == null){
            return map;
        } else{
            for (String key : map.keySet()) {
                newMap.put(key.toLowerCase(), map.get(key));
            }
        }
        return newMap;
    }
    
}